//
//  ZWLBaseViewController.swift
//  ZWLDY
//
//  Created by czh on 2016/10/12.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit

private let KMargin : CGFloat = 10
private let KItemW : CGFloat = (kScreenW - KMargin * 3) / 2
private let KItemH : CGFloat = KItemW * 3 / 4
private let kHighItemH = KItemW * 4 / 3

private let KRemCellID = "KRemCellID"
private let KRemHighhCellID = "KRemHighhCellID"
private let kHeaderViewID = "kHeaderViewID"
private let kHeaderViewH : CGFloat = 50

class ZWLBaseViewController: UIViewController {
    
    fileprivate lazy var RemVM : ZWLHomeVM = ZWLHomeVM()
    
        public lazy var collectionView : UICollectionView = { [weak self] in
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: KItemW, height: KItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = KMargin
        layout.sectionInset = UIEdgeInsets(top: 0, left: KMargin, bottom: 0, right: KMargin)
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        let F = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH - kStatusBarH - kNavigationBarH - KpagetitleH - kTabbarH)
        let collectionView = UICollectionView(frame: F, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register( UINib(nibName: "ZWLContentCell", bundle: nil), forCellWithReuseIdentifier: KRemCellID)
        collectionView.register( UINib(nibName: "ZWLContentHighCell", bundle: nil), forCellWithReuseIdentifier: KRemHighhCellID)
        // 注册头部视图
        collectionView.register(UINib(nibName: "ZWLHeaderCollectionViewCell", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        
        return collectionView
        }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        
        // 设置UI
        setupUI()

    }

   

}

// MARK:- 加载网络数据
extension ZWLBaseViewController {
    func loadData() {
        // 请求推荐数据
        RemVM.requestData {
            self.collectionView.reloadData()
            
        }
        
     
    }
}

// MARK:- collectionView的数据源和代理方法
extension ZWLBaseViewController : UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return RemVM.anchorGroups.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let group = RemVM.anchorGroups[section]
        
        return group.anchors.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // 取出模型对象
        let group = RemVM.anchorGroups[indexPath.section]
        let anchor = group.anchors[indexPath.item]
        
        if indexPath.section == 1 {
            var cell = ZWLContentHighCell()
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRemHighhCellID, for: indexPath) as! ZWLContentHighCell
            cell.anchor = anchor
            return cell
        } else {
            var cell = ZWLContentCell()
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRemCellID, for: indexPath) as! ZWLContentCell
            cell.anchor = anchor
            return cell
        }
        
    }
    
    // 返回一个头部视图
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath as IndexPath) as! ZWLHeaderCollectionViewCell
        
        headerView.group = RemVM.anchorGroups[indexPath.section]
        
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: KItemW, height: kHighItemH)
        }
        
        return CGSize(width: KItemW, height: KItemH)
    }
}

// MARK:- UI 布局
extension ZWLBaseViewController {
    fileprivate func setupUI() {
        
        view.addSubview(collectionView)
      
    }
}
